<?php

namespace Base\Model;

use Zend\Db\TableGateway\TableGateway;
use Zend\Paginator\Paginator;
use Zend\Paginator\Adapter\DbSelect;


class BaseTable {

    protected $tableGateway;

    protected $tableSelect;

    public function __construct(TableGateway $tableGateway) {
        $this->tableGateway = $tableGateway;
    }

    public function init($tableName = '', $dbName = '') {
        $this->tableSelect = $this->tableGateway->getSql()->select();
    }

    public function getPaginator($page = 1, $itemsPerPage = 10, $select = null) {
        if (!$select) {
            $select = $this->tableSelect;
        }
        if ($page) {
            $adapter = new DbSelect($select, $this->tableGateway->getAdapter());
            $paginator = new Paginator($adapter);
            $paginator->setCurrentPageNumber($page);
            $paginator->setItemCountPerPage($itemsPerPage);
            return $paginator;
        } else {
            $rowsData = $this->tableGateway->selectWith($select);
            return $rowsData;
        }
    }
}